package cn.edu.jnu.x2022101818;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.textfield.TextInputEditText;

public class MainActivity extends AppCompatActivity {
    private BowlingGame bowlingGame;

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        findViewById(R.id.button_calculate).setOnClickListener(v -> {
            TextInputEditText editText_rolls = findViewById(R.id.editText_rolls);
            if (editText_rolls.getText() == null) {
                return;
            }

            bowlingGame = new BowlingGame();
            String[] rollsArray = editText_rolls.getText().toString().split(",");
            for (String roll : rollsArray) {
                bowlingGame.roll(Integer.parseInt(roll));
            }

            TextView textView_totalScore = findViewById(R.id.textView_totalScore);
            String information = getString(R.string.total_score);
            textView_totalScore.setText(information + " " + bowlingGame.score());
        });
    }
}